package com.swing.gui.component.popups;

import base.JPopupMenuBase;
import com.swing.gui.APP;
import com.swing.gui.component.MiniPopup;
import com.swing.gui.component.popups.second.Dome;
import com.swing.gui.pages.Lock;
import com.swing.gui.Settings;
import com.swing.thread.TrayFlash;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 待锁的菜单（初始化）
 * 一级菜单栏
 *
 * @author Yuang, Gongming
 * @date 2022-01-24 11:30
 */
public class LockMenu extends JPopupMenu implements JPopupMenuBase {

    private static LockMenu lockMenu;

    private static JMenuItem lock, home, settings, exit;

    private static JCheckBoxMenuItem offMgs;

    public static LockMenu init() {
        if (lockMenu == null) {
            synchronized (LockMenu.class) {
                if (lockMenu == null) {
                    lockMenu = new LockMenu();
                    try {
                        lockMenu.instance();
                        lockMenu.style();
                        lockMenu.listener();
                        lockMenu.loading();
                    } catch (Exception e) {
                        throw new RuntimeException(LockMenu.class.getSimpleName() + " initialization failed");
                    }

                }
            }
        }
        return lockMenu;
    }

    @Override
    public void instance() {
        offMgs = new JCheckBoxMenuItem("关闭图标闪动");
        lock = new JMenuItem("锁住面板");
        home = new JMenuItem("打开主面板");
        settings = new JMenuItem("设置");
        exit = new JMenuItem("退出");
    }

    @Override
    public void style() {
        offMgs.setBackground(Color.WHITE);
        lock.setBackground(Color.WHITE);
        home.setBackground(Color.WHITE);
        settings.setBackground(Color.WHITE);
        exit.setBackground(Color.WHITE);
    }

    @Override
    public void listener() {
        offMgs.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!offMgs.isSelected()) {
                    new Thread(TrayFlash.init(true)).start();
                } else {
                    TrayFlash.isStop = false;
                }

            }
        });

        lock.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                MiniPopup.jPopupMenu = UnLockMenu.init();

                APP.init().setContentPane(Lock.init());

                APP.init().setVisible(true);
            }
        });

        home.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Settings.init().setVisible(false);
                //  不需要移除
//                SystemTray.getSystemTray().remove(com.swing.gui.component.MiniTray.init());
                APP.init().setVisible(true);
                APP.init().setExtendedState(JFrame.NORMAL);
                APP.init().toFront();
            }
        });

        settings.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                APP.init().setVisible(true);
                APP.init().setExtendedState(JFrame.NORMAL);

                Settings.init().setVisible(true);
                Settings.init().toFront();
            }
        });

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }

    @Override
    public void loading() {
        lockMenu.add(Dome.init());
        lockMenu.add(offMgs);
        lockMenu.add(lock);
        lockMenu.add(home);
        lockMenu.add(settings);
        lockMenu.addSeparator();
        lockMenu.add(exit);

    }

}
